import tkinter as tk
from tkinter import messagebox
import os
from src.core import items as items_core

def show_launch_screen(parent, item_manager):
    for widget in parent.winfo_children():
        widget.destroy()

    parent.config(bg="#E0F7FA") 

    BTN_COLOR = "#D7B899"
    BTN_HOVER = "#C6A583"
    BTN_FG = "white"
    BG_COLOR = "#E0F7FA"
    CARD_COLOR = "#FFFFFF"
    BORDER_COLOR = "#B8966D"
    TEXT_COLOR = "#4A3728"
    HIGHLIGHT_COLOR = "#8B5A4A"

    def center_window(win, w, h):
        win.update_idletasks()
        sw = win.winfo_screenwidth()
        sh = win.winfo_screenheight()
        x = int((sw - w) / 2)
        y = int((sh - h) / 2)
        win.geometry(f"{w}x{h}+{x}+{y}")
        try:
            win.resizable(False, False)
        except Exception:
            pass
        return None

    if not items_core.list_items(item_manager):
        try:
            items_core.clear(item_manager)
        except Exception:
            pass
        items_core.add_item(item_manager, "croissant", 2.5, "2024-01-15")

    def create_rounded_frame(parent, bg_color, border_color, padding=10):
        outer = tk.Frame(parent, bg=border_color, bd=0, relief=tk.FLAT)
        inner = tk.Frame(outer, bg=bg_color, bd=0, relief=tk.FLAT)
        inner.pack(padx=padding, pady=padding)
        return outer, inner

    def create_styled_button(parent, text, command, font_size=10, width=15):
        button = tk.Button(parent, text=text, command=command, 
                          bg=BTN_COLOR, fg=BTN_FG, 
                          font=("Arial", font_size, "bold"),
                          padx=15, pady=6,
                          width=width,
                          relief=tk.RAISED, bd=2,
                          activebackground=BTN_HOVER, 
                          activeforeground=BTN_FG)
        
        def on_enter(e):
            button.config(bg=BTN_HOVER)
        def on_leave(e):
            button.config(bg=BTN_COLOR)
        button.bind("<Enter>", on_enter)
        button.bind("<Leave>", on_leave)
        return button

    def create_styled_entry(parent, width=30):
        entry = tk.Entry(parent, width=width, font=("Arial", 11),
                        bd=2, relief=tk.SUNKEN, highlightthickness=1,
                        highlightbackground=BORDER_COLOR,
                        highlightcolor=HIGHLIGHT_COLOR)
        return entry

    def open_view_items():
        win = tk.Toplevel(parent)
        win.title("Bakery Items")
        win.configure(bg=BG_COLOR)
        win.resizable(True, True)
        center_window(win, 600, 550)
        
        header_frame = tk.Frame(win, bg=BG_COLOR)
        header_frame.pack(fill=tk.X, padx=15, pady=10)
        
        header_icon = tk.Label(header_frame, text="🥐", font=("Arial", 24), bg=BG_COLOR)
        header_icon.pack(side=tk.LEFT, padx=10)
        header_title = tk.Label(header_frame, text="Fresh Bakery Items", font=("Arial", 18, "bold"), fg=TEXT_COLOR, bg=BG_COLOR)
        header_title.pack(side=tk.LEFT)

        search_frame = tk.Frame(win, bg=BG_COLOR)
        search_frame.pack(fill=tk.X, padx=20, pady=10)
        
        search_label = tk.Label(search_frame, text="Search:", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=BG_COLOR)
        search_label.pack(side=tk.LEFT, padx=(0, 10), pady=5)
        
        search_entry = create_styled_entry(search_frame, width=40)
        search_entry.pack(side=tk.LEFT, padx=(0, 10), fill=tk.X, expand=True)
        
        search_button = create_styled_button(search_frame, "Search", None, font_size=10)
        search_button.pack(side=tk.LEFT, padx=5)
        
        def do_search_in_view():
            q = search_entry.get().strip().lower()
            for widget in scroll.winfo_children():
                widget.destroy()
            

            has_items = False
            for it in items_core.list_items(item_manager):
                text = f"ID:{it['id']} | {it['name']} | ${it['price']} | {it['production_date']}"
                if q in it['name'].lower() or q in text.lower():
                    has_items = True
                    card_outer, card_inner = create_rounded_frame(scroll, CARD_COLOR, BORDER_COLOR, padding=1)
                    card_outer.pack(fill=tk.X, pady=8, padx=5)
                    
                    content_frame = tk.Frame(card_inner, bg=CARD_COLOR)
                    content_frame.pack(fill=tk.X, expand=True)
                    
                    icon_label = tk.Label(content_frame, textvariable=big_icon_var, font=("Arial", 32), bg=CARD_COLOR)
                    icon_label.pack(side=tk.LEFT, padx=15, pady=10)
                    
                    info = tk.Frame(content_frame, bg=CARD_COLOR)
                    info.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=10, pady=10)
                    
                    id_frame = tk.Frame(info, bg=HIGHLIGHT_COLOR)
                    id_frame.pack(anchor="w", pady=(0, 5))
                    tk.Label(id_frame, text=f"ID: {it['id']}", bg=HIGHLIGHT_COLOR, font=("Arial", 10, "bold"), fg="white").pack(padx=8, pady=2)
                    
                    tk.Label(info, text=f"Name: {it['name']}", bg=CARD_COLOR, font=("Arial", 13, "bold"), fg=TEXT_COLOR).pack(anchor="w", pady=(2, 2))
                    
                    bottom = tk.Frame(info, bg=CARD_COLOR)
                    bottom.pack(anchor="w", pady=(5, 0), fill=tk.X)
                    
                    price_frame = tk.Frame(bottom, bg="#F0E6D2", bd=1, relief=tk.RAISED)
                    price_frame.pack(side=tk.LEFT, padx=(0, 15))
                    tk.Label(price_frame, text=f"Price: ${it['price']}", bg="#F0E6D2", font=("Arial", 11, "bold"), fg="#8B5A2B").pack(padx=10, pady=3)
                    
                    date_frame = tk.Frame(bottom, bg="#E6F3E6", bd=1, relief=tk.RAISED)
                    date_frame.pack(side=tk.LEFT)
                    tk.Label(date_frame, text=f"Date: {it['production_date']}", bg="#E6F3E6", font=("Arial", 11), fg="#3A6E3A").pack(padx=10, pady=3)
            
            if not has_items:
                no_items_frame = tk.Frame(scroll, bg=BG_COLOR)
                no_items_frame.pack(pady=30)
                tk.Label(no_items_frame, text="No items found.", font=("Arial", 12), fg="#8B0000", bg=BG_COLOR).pack()
                tk.Label(no_items_frame, text="🍞", font=("Arial", 36), bg=BG_COLOR).pack(pady=10)
        
        search_button.config(command=do_search_in_view)
        
        container = tk.Frame(win, bg=BG_COLOR)
        container.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
        
        style_frame = tk.Frame(container, bg=BORDER_COLOR, bd=1)
        style_frame.pack(side=tk.RIGHT, fill=tk.Y, padx=(0, 5))
        
        vsb = tk.Scrollbar(style_frame, orient="vertical", bg=BTN_COLOR, troughcolor="#F0E6D2", width=12, bd=1, relief=tk.FLAT)
        vsb.pack(fill=tk.Y, expand=True, padx=2, pady=2)
        
        canvas = tk.Canvas(container, bg=BG_COLOR, highlightthickness=0)
        scroll = tk.Frame(canvas, bg=BG_COLOR)
        
        scroll.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
        canvas_window = canvas.create_window((0, 0), window=scroll, anchor="nw")
        canvas.configure(yscrollcommand=vsb.set)
        
        def on_mousewheel(event):
            canvas.yview_scroll(int(-1*(event.delta/120)), "units")
        canvas.bind_all("<MouseWheel>", on_mousewheel)
        
        canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        vsb.config(command=canvas.yview)

        items = items_core.list_items(item_manager)
        if not items:
            no_items_frame = tk.Frame(scroll, bg=BG_COLOR)
            no_items_frame.pack(pady=40)
            tk.Label(no_items_frame, text="No items available.", font=("Arial", 14), fg="#8B0000", bg=BG_COLOR).pack()
            tk.Label(no_items_frame, text="🥐 Add new items to get started! 🥐", font=("Arial", 12), fg=TEXT_COLOR, bg=BG_COLOR).pack(pady=15)
            return

        icon_frames = ["🥐", "🥖", "🥯", "🍰", "🍪"]
        big_icon_var = tk.StringVar(value=icon_frames[0])
        idx = {"i": 0}
        def tick():
            idx["i"] = (idx["i"] + 1) % len(icon_frames)
            big_icon_var.set(icon_frames[idx["i"]])
            win.after(600, tick) 

        for it in items:
            card_outer, card_inner = create_rounded_frame(scroll, CARD_COLOR, BORDER_COLOR, padding=1)
            card_outer.pack(fill=tk.X, pady=8, padx=5)
            
            content_frame = tk.Frame(card_inner, bg=CARD_COLOR)
            content_frame.pack(fill=tk.X, expand=True)
            
            icon_label = tk.Label(content_frame, textvariable=big_icon_var, font=("Arial", 32), bg=CARD_COLOR)
            icon_label.pack(side=tk.LEFT, padx=15, pady=10)
            
            info = tk.Frame(content_frame, bg=CARD_COLOR)
            info.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=10, pady=10)
            
            id_frame = tk.Frame(info, bg=HIGHLIGHT_COLOR)
            id_frame.pack(anchor="w", pady=(0, 5))
            tk.Label(id_frame, text=f"ID: {it['id']}", bg=HIGHLIGHT_COLOR, font=("Arial", 10, "bold"), fg="white").pack(padx=8, pady=2)
            
            tk.Label(info, text=f"Name: {it['name']}", bg=CARD_COLOR, font=("Arial", 13, "bold"), fg=TEXT_COLOR).pack(anchor="w", pady=(2, 2))
            
            bottom = tk.Frame(info, bg=CARD_COLOR)
            bottom.pack(anchor="w", pady=(5, 0), fill=tk.X)
            
            price_frame = tk.Frame(bottom, bg="#F0E6D2", bd=1, relief=tk.RAISED)
            price_frame.pack(side=tk.LEFT, padx=(0, 15))
            tk.Label(price_frame, text=f"Price: ${it['price']}", bg="#F0E6D2", font=("Arial", 11, "bold"), fg="#8B5A2B").pack(padx=10, pady=3)
            
            date_frame = tk.Frame(bottom, bg="#E6F3E6", bd=1, relief=tk.RAISED)
            date_frame.pack(side=tk.LEFT)
            tk.Label(date_frame, text=f"Date: {it['production_date']}", bg="#E6F3E6", font=("Arial", 11), fg="#3A6E3A").pack(padx=10, pady=3)
        
        return None

    def open_add_item():
        win = tk.Toplevel(parent)
        win.title("Add New Bakery Item")
        win.configure(bg=BG_COLOR)
        win.resizable(False, False)
        center_window(win, 420, 340)

        header_frame = tk.Frame(win, bg=BG_COLOR)
        header_frame.pack(fill=tk.X, padx=20, pady=15)
        
        header_icon = tk.Label(header_frame, text="🍰", font=("Arial", 24), bg=BG_COLOR)
        header_icon.pack(side=tk.LEFT, padx=10)
        header_title = tk.Label(header_frame, text="Create New Item", font=("Arial", 16, "bold"), fg=TEXT_COLOR, bg=BG_COLOR)
        header_title.pack(side=tk.LEFT)

        form_frame_outer, form_frame_inner = create_rounded_frame(win, CARD_COLOR, BORDER_COLOR, padding=5)
        form_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
        
        name_frame = tk.Frame(form_frame_inner, bg=CARD_COLOR)
        name_frame.pack(fill=tk.X, pady=8, padx=10)
        tk.Label(name_frame, text="Name:", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=CARD_COLOR).pack(anchor="w", pady=(0, 3))
        name_e = create_styled_entry(name_frame, width=45)
        name_e.pack(fill=tk.X, expand=True)

        price_frame = tk.Frame(form_frame_inner, bg=CARD_COLOR)
        price_frame.pack(fill=tk.X, pady=8, padx=10)
        tk.Label(price_frame, text="Price ($):", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=CARD_COLOR).pack(anchor="w", pady=(0, 3))
        price_e = create_styled_entry(price_frame, width=45)
        price_e.pack(fill=tk.X, expand=True)

        date_frame = tk.Frame(form_frame_inner, bg=CARD_COLOR)
        date_frame.pack(fill=tk.X, pady=8, padx=10)
        tk.Label(date_frame, text="Production Date (YYYY-MM-DD):", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=CARD_COLOR).pack(anchor="w", pady=(0, 3))
        date_e = create_styled_entry(date_frame, width=45)
        date_e.pack(fill=tk.X, expand=True)
        
        help_frame = tk.Frame(form_frame_inner, bg=CARD_COLOR)
        help_frame.pack(fill=tk.X, pady=5, padx=10)
        tk.Label(help_frame, text="Example: 2024-01-15", font=(
            "Arial", 9, "italic"), fg="#666666", bg=CARD_COLOR).pack(anchor="w")
        
        def do_create():
            try:
                name = name_e.get().strip()
                if not name:
                    tk.messagebox.showerror("Error", "Name cannot be empty.")
                    return
                
                price = float(price_e.get())
                if price <= 0:
                    tk.messagebox.showerror("Error", "Price must be positive.")
                    return
                    
                date = date_e.get().strip()
                import re
                if not re.match(r'^\d{4}-\d{2}-\d{2}$', date):
                    tk.messagebox.showerror("Error", "Invalid date format. Use YYYY-MM-DD.")
                    return
                    
                ok, msg = items_core.add_item(item_manager, name, price, date)
                
                success_window = tk.Toplevel(win)
                success_window.title("Success!")
                success_window.configure(bg=BG_COLOR)
                success_window.geometry("300x180")
                success_window.resizable(False, False)
                
                x = win.winfo_x() + (win.winfo_width() // 2) - 150
                y = win.winfo_y() + (win.winfo_height() // 2) - 90
                success_window.geometry(f"300x180+{x}+{y}")
                
                success_frame_outer, success_frame_inner = create_rounded_frame(success_window, "#E6F7E6", "#8BC34A", padding=5)
                success_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
                
                tk.Label(success_frame_inner, text="🎉", font=("Arial", 40), bg="#E6F7E6").pack(pady=10)
                tk.Label(success_frame_inner, text="Item Added Successfully!", font=("Arial", 12, "bold"), fg="#2E7D32", bg="#E6F7E6").pack()
                
                def close_windows():
                    success_window.destroy()
                    win.destroy()
                
                tk.Button(success_frame_inner, text="OK", command=close_windows, 
                         bg="#8BC34A", fg="white", font=("Arial", 10, "bold"), 
                         padx=20, pady=5).pack(pady=15)
                
            except ValueError:
                error_window = tk.Toplevel(win)
                error_window.title("Error")
                error_window.configure(bg=BG_COLOR)
                error_window.geometry("300x150")
                error_window.resizable(False, False)
                
                x = win.winfo_x() + (win.winfo_width() // 2) - 150
                y = win.winfo_y() + (win.winfo_height() // 2) - 75
                error_window.geometry(f"300x150+{x}+{y}")
                
                error_frame_outer, error_frame_inner = create_rounded_frame(error_window, "#FFEBEE", "#F44336", padding=5)
                error_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
                
                tk.Label(error_frame_inner, text="❌", font=("Arial", 30), bg="#FFEBEE").pack(pady=5)
                tk.Label(error_frame_inner, text="Invalid price format.", font=("Arial", 11, "bold"), fg="#C62828", bg="#FFEBEE").pack()
                tk.Label(error_frame_inner, text="Please enter a valid number.", font=("Arial", 10), fg="#C62828", bg="#FFEBEE").pack()
                
                tk.Button(error_frame_inner, text="OK", command=error_window.destroy, 
                         bg="#F44336", fg="white", font=("Arial", 10, "bold"), 
                         padx=20, pady=3).pack(pady=10)
        
        center_window(win, 450, 400)
        
        button_container = tk.Frame(win, bg=BG_COLOR)
        button_container.pack(fill=tk.X, padx=40, pady=20)
        
        create_button = tk.Button(button_container, text="Create Item", command=do_create,
                               bg=BTN_COLOR, fg=BTN_FG,
                               font=("Arial", 12, "bold"),
                               width=25,
                               padx=30, pady=10,
                               relief=tk.RAISED, bd=3)
        
        create_button.pack(anchor="center", expand=True)

        name_e.focus_set()
        
        return None

    def open_delete_item():
        win = tk.Toplevel(parent)
        win.title("Delete Bakery Item")
        win.configure(bg=BG_COLOR)
        win.resizable(False, False)
        center_window(win, 400, 280)

        header_frame = tk.Frame(win, bg=BG_COLOR)
        header_frame.pack(fill=tk.X, padx=20, pady=15)
        
        header_icon = tk.Label(header_frame, text="⚠️", font=("Arial", 24), bg=BG_COLOR)
        header_icon.pack(side=tk.LEFT, padx=10)
        header_title = tk.Label(header_frame, text="Delete Item", font=("Arial", 16, "bold"), fg="#C62828", bg=BG_COLOR)
        header_title.pack(side=tk.LEFT)

        warning_frame_outer, warning_frame_inner = create_rounded_frame(win, "#FFF3E0", "#FF9800", padding=5)
        warning_frame_outer.pack(fill=tk.X, padx=20, pady=10)
        
        warning_text = "This action cannot be undone. Deleting an item will remove it permanently from your bakery menu."
        tk.Label(warning_frame_inner, text=warning_text, font=("Arial", 10), fg="#E65100", bg="#FFF3E0", wraplength=320).pack(padx=10, pady=10)

        id_frame = tk.Frame(win, bg=BG_COLOR)
        id_frame.pack(fill=tk.X, padx=20, pady=10)
        
        tk.Label(id_frame, text="Enter ID to delete:", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=BG_COLOR).pack(anchor="w", pady=(0, 5))
        id_e = create_styled_entry(id_frame, width=40)
        id_e.pack(fill=tk.X, expand=True)
        
        def do_delete():
            try:
                iid = int(id_e.get())

                confirm_window = tk.Toplevel(win)
                confirm_window.title("Confirm Deletion")
                confirm_window.configure(bg=BG_COLOR)
                confirm_window.geometry("400x280")  
                confirm_window.resizable(False, False)

                x = win.winfo_x() + (win.winfo_width() // 2) - 200
                y = win.winfo_y() + (win.winfo_height() // 2) - 140
                confirm_window.geometry(f"400x280+{x}+{y}")
                
                icon_label = tk.Label(confirm_window, text="❓", font=("Arial", 50), bg="#FFEBEE")
                icon_label.pack(pady=10)
                
                confirm_text = f"Are you sure you want to delete item with ID {iid}?"
                text_label = tk.Label(confirm_window, text=confirm_text, 
                         font=("Arial", 12, "bold"), fg="#C62828", bg="#FFEBEE", wraplength=350)
                text_label.pack(pady=10)
                
                button_frame = tk.Frame(confirm_window, bg="#FFEBEE")
                button_frame.pack(pady=20, padx=40, fill=tk.X)
                
                def confirm_deletion():
                    ok, msg = items_core.delete_item(item_manager, iid)
                    confirm_window.destroy()
                    
                    if ok:
                        success_window = tk.Toplevel(win)
                        success_window.title("Deleted")
                        success_window.configure(bg=BG_COLOR)
                        success_window.geometry("300x180")
                        success_window.resizable(False, False)
                        
                        x = win.winfo_x() + (win.winfo_width() // 2) - 150
                        y = win.winfo_y() + (win.winfo_height() // 2) - 90
                        success_window.geometry(f"300x180+{x}+{y}")
                        
                        success_frame_outer, success_frame_inner = create_rounded_frame(success_window, "#E6F7E6", "#8BC34A", padding=5)
                        success_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
                        
                        tk.Label(success_frame_inner, text="✅", font=("Arial", 40), bg="#E6F7E6").pack(pady=10)
                        tk.Label(success_frame_inner, text="Item Deleted Successfully!", font=("Arial", 12, "bold"), fg="#2E7D32", bg="#E6F7E6").pack()
                        
                        def close_and_exit():
                            success_window.destroy()
                            win.destroy()
                        
                        tk.Button(success_frame_inner, text="OK", command=close_and_exit, 
                                 bg="#8BC34A", fg="white", font=("Arial", 10, "bold"), 
                                 padx=20, pady=5).pack(pady=15)
                    else:
                        error_window = tk.Toplevel(win)
                        error_window.title("Error")
                        error_window.configure(bg=BG_COLOR)
                        error_window.geometry("350x180")
                        error_window.resizable(False, False)
                        
                        x = win.winfo_x() + (win.winfo_width() // 2) - 175
                        y = win.winfo_y() + (win.winfo_height() // 2) - 90
                        error_window.geometry(f"350x180+{x}+{y}")
                        
                        error_frame_outer, error_frame_inner = create_rounded_frame(error_window, "#FFEBEE", "#F44336", padding=5)
                        error_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
                        
                        tk.Label(error_frame_inner, text="❌", font=("Arial", 30), bg="#FFEBEE").pack(pady=5)
                        tk.Label(error_frame_inner, text="Cannot delete item.", font=("Arial", 11, "bold"), fg="#C62828", bg="#FFEBEE").pack()
                        tk.Label(error_frame_inner, text=msg, font=("Arial", 10), fg="#C62828", bg="#FFEBEE", wraplength=280).pack()
                        
                        tk.Button(error_frame_inner, text="OK", command=error_window.destroy, 
                                 bg="#F44336", fg="white", font=("Arial", 10, "bold"), 
                                 padx=20, pady=3).pack(pady=10)
                
                confirm_btn = tk.Button(button_frame, text="Confirm Delete", command=confirm_deletion,
                         bg="#F44336", fg="white", font=("Arial", 12, "bold"),
                         width=15, padx=10, pady=8, relief=tk.RAISED, bd=2)
                confirm_btn.pack(side=tk.LEFT, padx=15, expand=True)
                
                cancel_btn = tk.Button(button_frame, text="Cancel", command=confirm_window.destroy,
                         bg=BTN_COLOR, fg="white", font=("Arial", 12, "bold"),
                         width=10, padx=10, pady=8, relief=tk.RAISED, bd=2)
                cancel_btn.pack(side=tk.LEFT, padx=15, expand=True)
                
            except ValueError:
                error_window = tk.Toplevel(win)
                error_window.title("Error")
                error_window.configure(bg=BG_COLOR)
                error_window.geometry("300x150")
                error_window.resizable(False, False)
                
                x = win.winfo_x() + (win.winfo_width() // 2) - 150
                y = win.winfo_y() + (win.winfo_height() // 2) - 75
                error_window.geometry(f"300x150+{x}+{y}")
                
                error_frame_outer, error_frame_inner = create_rounded_frame(error_window, "#FFEBEE", "#F44336", padding=5)
                error_frame_outer.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
                
                tk.Label(error_frame_inner, text="❌", font=("Arial", 30), bg="#FFEBEE").pack(pady=5)
                tk.Label(error_frame_inner, text="Invalid ID format.", font=("Arial", 11, "bold"), fg="#C62828", bg="#FFEBEE").pack()
                tk.Label(error_frame_inner, text="Please enter a valid number.", font=("Arial", 10), fg="#C62828", bg="#FFEBEE").pack()
                
                tk.Button(error_frame_inner, text="OK", command=error_window.destroy, 
                         bg="#F44336", fg="white", font=("Arial", 10, "bold"), 
                         padx=20, pady=3).pack(pady=10)
        
        button_frame = tk.Frame(win, bg=BG_COLOR)
        button_frame.pack(pady=15)
        
        delete_button = tk.Button(button_frame, text="Delete Item", command=do_delete, 
                                 bg="#F44336", fg="white", font=("Arial", 11, "bold"),
                                 padx=15, pady=6, activebackground="#D32F2F")
        delete_button.pack(padx=20)

        def on_enter(e):
            delete_button.config(bg="#D32F2F")
        def on_leave(e):
            delete_button.config(bg="#F44336")
        delete_button.bind("<Enter>", on_enter)
        delete_button.bind("<Leave>", on_leave)

        id_e.focus_set()
        
        return None

    def do_save():
        from src.storage import file_manager as fm
        ok, msg = fm.save_items(item_manager)
        tk.messagebox.showinfo("Save", msg)
        return None

    def do_load():
        try:
            from src.storage import file_manager as fm
            ok, msg = fm.load_items(item_manager)
            if ok:
                tk.messagebox.showinfo("Load", "Items loaded successfully.")
            else:
                tk.messagebox.showwarning("Load", msg)
        except Exception as e:
            tk.messagebox.showerror("Load", str(e))
        return None

    def open_search():
        win = tk.Toplevel(parent)
        win.title("Search Bakery Items")
        win.configure(bg=BG_COLOR)
        win.resizable(True, True)
        center_window(win, 550, 450)
        
        header_frame = tk.Frame(win, bg=BG_COLOR)
        header_frame.pack(fill=tk.X, padx=20, pady=15)
        
        header_icon = tk.Label(header_frame, text="🔍", font=("Arial", 20), bg=BG_COLOR)
        header_icon.pack(side=tk.LEFT, padx=10)
        header_title = tk.Label(header_frame, text="Search Menu Items", font=("Arial", 16, "bold"), fg=TEXT_COLOR, bg=BG_COLOR)
        header_title.pack(side=tk.LEFT)
        
        search_frame_outer, search_frame_inner = create_rounded_frame(win, CARD_COLOR, BORDER_COLOR, padding=5)
        search_frame_outer.pack(fill=tk.X, padx=20, pady=10)
        
        search_label = tk.Label(search_frame_inner, text="Keyword:", font=("Arial", 11, "bold"), fg=TEXT_COLOR, bg=CARD_COLOR)
        search_label.pack(anchor="w", pady=(0, 5))
        
        search_input_frame = tk.Frame(search_frame_inner, bg=CARD_COLOR)
        search_input_frame.pack(fill=tk.X, expand=True)
        
        kw = create_styled_entry(search_input_frame, width=50)
        kw.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 10))
        
        search_button = create_styled_button(search_input_frame, "Search", None, font_size=10)
        search_button.pack(side=tk.LEFT, padx=5)
        
        result_frame = tk.Frame(win, bg=BG_COLOR)
        result_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=10)
        
        listbox_frame_outer, listbox_frame_inner = create_rounded_frame(result_frame, CARD_COLOR, BORDER_COLOR, padding=3)
        listbox_frame_outer.pack(fill=tk.BOTH, expand=True)

        result_title_frame = tk.Frame(listbox_frame_inner, bg=CARD_COLOR)
        result_title_frame.pack(fill=tk.X, pady=5, padx=5)
        tk.Label(result_title_frame, text="Search Results:", font=("Arial", 10, "bold"), fg=TEXT_COLOR, bg=CARD_COLOR).pack(anchor="w")

        container = tk.Frame(listbox_frame_inner, bg=CARD_COLOR)
        container.pack(fill=tk.BOTH, expand=True)

        style_frame = tk.Frame(container, bg=BORDER_COLOR, bd=1)
        style_frame.pack(side=tk.RIGHT, fill=tk.Y, padx=(0, 5))
        
        vsb = tk.Scrollbar(style_frame, orient="vertical", bg=BTN_COLOR, troughcolor="#F0E6D2", width=12, bd=1, relief=tk.FLAT)
        vsb.pack(fill=tk.Y, expand=True, padx=2, pady=2)

        result = tk.Listbox(container, 
                          font=("Arial", 10),
                          bg=CARD_COLOR,
                          fg=TEXT_COLOR,
                          bd=0,
                          relief=tk.FLAT,
                          selectbackground=BTN_COLOR,
                          selectforeground="white",
                          activestyle="none",
                          highlightthickness=1,
                          highlightbackground=BORDER_COLOR,
                          highlightcolor=HIGHLIGHT_COLOR,
                          yscrollcommand=vsb.set)
        
        result.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        vsb.config(command=result.yview)

        def on_mousewheel(event):
            result.yview_scroll(int(-1*(event.delta/120)), "units")
        result.bind_all("<MouseWheel>", on_mousewheel)

        result.insert(tk.END, "Type in a keyword above and click Search to find items...")
        result.itemconfig(0, {'fg': '#999999'})
        
        def do_search():
            q = kw.get().strip().lower()
            result.delete(0, tk.END)

            result.insert(tk.END, "Searching...")
            result.itemconfig(0, {'fg': '#999999'})
            win.update()

            win.after(300)
            
            result.delete(0, tk.END)
            found_items = []
            
            for it in items_core.list_items(item_manager):
                text = f"ID: {it['id']} | {it['name']} | ${it['price']} | {it['production_date']}"
                if q in it['name'].lower() or q in str(it['id']).lower() or q in str(it['price']).lower() or q in it['production_date'].lower():
                    found_items.append(text)
            
            if not found_items:
                result.insert(tk.END, 'No results found for your search.')
                result.itemconfig(0, {'fg': '#8B0000'})
            else:
                result.insert(tk.END, f"Found {len(found_items)} item(s):")
                result.itemconfig(0, {'fg': '#336633', 'font': ("Arial", 10, "bold")})
                result.itemconfig(0, {'selectbackground': CARD_COLOR, 'selectforeground': '#336633'})

                for item in found_items:
                    result.insert(tk.END, item)
        
        search_button.config(command=do_search)

        kw.bind("<Return>", lambda event: do_search())
        
        kw.focus_set()
        
        return None

    title_frame = tk.Frame(parent, bg="#E0F7FA")
    title_frame.pack(pady=20)

    croissant_icon = tk.Label(title_frame, text="🥐", font=("Arial", 24), bg="#E0F7FA")
    croissant_icon.pack(side=tk.LEFT, padx=10)
    title = tk.Label(title_frame, text="Croissant Bakery", font=("Arial", 28, "bold"), bg="#E0F7FA")
    title.pack(side=tk.LEFT)
    subtitle = tk.Label(parent, text="Welcome to Our Fresh Bakery!", font=("Arial", 14), bg="#E0F7FA")
    subtitle.pack(pady=5)

    button_frame = tk.Frame(parent, bg="#E0F7FA")
    button_frame.pack(pady=20)
    view_menu_btn = tk.Button(
        button_frame, text="View Menu", font=("Arial", 14),
        bg=BTN_COLOR, fg=BTN_FG, padx=30, pady=10,
        command=open_view_items
    )
    view_menu_btn.pack(pady=5)
    add_item_btn = tk.Button(
        button_frame, text="Add New Item", font=("Arial", 14),
        bg=BTN_COLOR, fg=BTN_FG, padx=30, pady=10,
        command=open_add_item
    )
    add_item_btn.pack(pady=5)

    action_frame = tk.Frame(parent, bg="#E0F7FA")
    action_frame.pack(pady=6)
    tk.Button(action_frame, text="Delete Item", command=open_delete_item,
              bg=BTN_COLOR, fg=BTN_FG, padx=16, pady=6).pack(side=tk.LEFT, padx=6)
    tk.Button(action_frame, text="Save Items", command=do_save,
              bg=BTN_COLOR, fg=BTN_FG, padx=16, pady=6).pack(side=tk.LEFT, padx=6)
    tk.Button(action_frame, text="Load Items", command=do_load,
              bg=BTN_COLOR, fg=BTN_FG, padx=16, pady=6).pack(side=tk.LEFT, padx=6)

    canvas = tk.Canvas(parent, width=600, height=350, bg="#E0F7FA", highlightthickness=0)
    canvas.pack(pady=10)

    canvas.create_polygon(
        0, 300, 600, 300, 600, 350, 0, 350,
        fill="#A5D6A7", outline=""
    )

    cx = 300
    store_w = 440
    store_left = cx - store_w // 2
    store_right = cx + store_w // 2

    canvas.create_rectangle(store_left, 110, store_right, 310, fill="#E8CDA9", outline="#B8966D", width=3)

    roof_h = 36
    canvas.create_polygon(store_left-22, 110, store_right+22, 110, store_right, 110-roof_h,
                          store_left, 110-roof_h, fill="#7D4634", outline="#5A3A2D", width=3)
    canvas.create_line(store_left-16, 110-12, store_right+16, 110-12, fill="#FFFFFF", width=6)
    canvas.create_line(store_left-10, 110-24, store_right+10, 110-24, fill="#FFFFFF", width=6)

    canvas.create_rectangle(cx-75, 110-roof_h-6, cx+75, 110-roof_h+20, fill="#F7F1E7", outline="#5A3A2D", width=3)
    canvas.create_text(cx, 110-roof_h+7, text="BAKERY", font=("Arial", 20, "bold"), fill="#2F2A26")

    scallop_top = 110
    scallop_bottom = scallop_top + 22
    step = 26
    colors = ["#8B5A4A", "#FFFFFF"]
    x = store_left
    i = 0
    while x < store_right:
        canvas.create_arc(x-13, scallop_top, x+13, scallop_bottom, start=0, extent=180,
                          style=tk.CHORD, fill=colors[i % 2], outline="#5A3A2D", width=2)
        x += step
        i += 1

    shelf_left = store_left + 40
    shelf_right = cx + 70
    shelf_top = 140
    shelf_bottom = 290
    canvas.create_rectangle(shelf_left-8, shelf_top-8, shelf_right+8, shelf_bottom+8,
                            fill="#725643", outline="#2F2A26", width=3)
    canvas.create_rectangle(shelf_left, shelf_top, shelf_right, shelf_bottom,
                            fill="#354B63", outline="#2F2A26", width=0)
    for y in (shelf_top+45, shelf_top+95):
        canvas.create_line(shelf_left, y, shelf_right, y, fill="#2F2A26", width=6)

    for x in (shelf_left + (shelf_right-shelf_left)//3, shelf_left + 2*(shelf_right-shelf_left)//3):
        canvas.create_line(x, shelf_top, x, shelf_bottom, fill="#2F2A26", width=4)

    door_left = cx + 110
    door_right = door_left + 88
    canvas.create_rectangle(door_left, 160, door_right, 300, fill="#7A665C", outline="#3E3E3E", width=3)

    canvas.create_oval(door_left+18, 225, door_left+30, 237, fill="#CFC4BE", outline="")

    awn_top = 140
    stripe_w = 22
    colors = ["#8B5A4A", "#FFFFFF"]
    for i in range(5):
        canvas.create_rectangle(door_left-12 + i*stripe_w, awn_top, door_left-12 + (i+1)*stripe_w, 160,
                                fill=colors[i % 2], outline="#3E3E3E")
    canvas.create_polygon(door_left-12, awn_top, door_right+12, awn_top, door_right, 160, door_left, 160,
                          fill="", outline="#3E3E3E", width=3)

    sx = (door_left + door_right)//2
    canvas.create_line(sx-12, 172, sx, 160, fill="#D3CEC9", width=2)
    canvas.create_line(sx+12, 172, sx, 160, fill="#D3CEC9", width=2)
    canvas.create_rectangle(sx-32, 182, sx+32, 200, fill="#FFFFFF", outline="#3E3E3E", width=2)
    canvas.create_text(sx, 191, text="open", font=("Arial", 12, "bold"), fill="#3E3E3E")

    def draw_street_lamp(base_x):
        canvas.create_polygon(base_x-18, 120, base_x+18, 120, base_x+14, 148, base_x-14, 148,
                              fill="#FFE8A3", outline="#3E3E3E", width=3)
        canvas.create_arc(base_x-20, 108, base_x+20, 128, start=0, extent=180,
                          style=tk.CHORD, fill="#3E3E3E", outline="#3E3E3E")
        canvas.create_line(base_x, 120, base_x, 148, fill="#3E3E3E", width=3)
        canvas.create_rectangle(base_x-6, 148, base_x+6, 320, fill="#555555", outline="#3E3E3E", width=3)
        canvas.create_rectangle(base_x-12, 320, base_x+12, 336, fill="#555555", outline="#3E3E3E", width=3)
        canvas.create_rectangle(base_x-22, 336, base_x+22, 346, fill="#777777", outline="#3E3E3E", width=3)

    draw_street_lamp(store_left-60)
    draw_street_lamp(store_right+60)

    window_left = shelf_left
    window_right = shelf_right
    cell_w = (window_right - window_left) / 3
    cell_h = (280 - 120) / 3

    y_offset = 8
    def cell_center(c, r):
        x = window_left + cell_w * (c + 0.5)
        y = 120 + cell_h * (r + 0.5) + y_offset
        return x, y

    def cell_bounds(c, r):
        x0 = window_left + cell_w * c
        x1 = window_left + cell_w * (c + 1)
        y0 = 120 + cell_h * r
        y1 = 120 + cell_h * (r + 1)
        return x0, y0, x1, y1

    x0, y0, x1, y1 = cell_bounds(0, 0)
    cake_pad = 8
    
    cake_y_top = y0+cake_pad+4
    cake_y_bottom = y0+cake_pad+32
    
    canvas.create_rectangle(x0+cake_pad, cake_y_top+2, x1-cake_pad, cake_y_bottom,
                            fill="#9B7FCC", outline="")

    canvas.create_rectangle(x0+cake_pad, cake_y_top, x1-cake_pad, cake_y_bottom-2,
                            fill="#BFA8F2", outline="#2F2A26")

    for i in range(3):
        canvas.create_line(x0+cake_pad+2, cake_y_top+8+i*6, x1-cake_pad-2, cake_y_top+8+i*6, 
                          fill="#C9B4F5", width=1, dash=(2,1))
    
    drip_y = cake_y_top+12
    for dx in (0, 18, 36):
        drip_x = x0+cake_pad+8+dx
        canvas.create_polygon(
            drip_x-2, drip_y,
            drip_x+2, drip_y,
            drip_x+4, drip_y+16,
            drip_x-4, drip_y+16,
            fill="#E6D9FF", outline="")

    canvas.create_rectangle(x0+cake_pad, drip_y-8, x1-cake_pad, drip_y-1,
                           fill="#E6D9FF", outline="")

    for i in range(5):
        canvas.create_oval(x0+cake_pad+8+i*12, drip_y-12, x0+cake_pad+12+i*12, drip_y-8,
                          fill="#FFFFFF", outline="")


    x0, y0, x1, y1 = cell_bounds(1, 0)
    cx, cy = (x0+x1)/2, (y0+y1)/2

    canvas.create_oval(cx-20, cy-14, cx+20, cy+14, fill="#E6D9B8", outline="#2F2A26")

    for i in range(3):
        canvas.create_arc(cx-18, cy-12+i*4, cx+18, cy+12+i*4, start=180, extent=180,
                          outline="#D6C4A8", width=1)
    
    canvas.create_oval(cx-16, cy-18, cx+16, cy+14, fill="#F8F8F8", outline="#2F2A26")

    for r in range(6, 16, 3):
        canvas.create_arc(cx-r, cy-r, cx+r, cy+r, start=135, extent=270,
                          outline="#E8E8E8", width=1.5)
    

    red_top = canvas.create_oval(cx-14, y0+6, cx+14, y0+32, fill="#F26D6D", outline="#2F2A26")
    # 红顶纹理
    for i in range(5):
        canvas.create_arc(cx-12, y0+8+i*4, cx+12, y0+14+i*4, start=180, extent=180,
                          outline="#E25A5A", width=1)
    # 点缀糖粒
    for i in range(8):
        angle = i * 45
        import math
        dot_x = cx + 8 * math.cos(math.radians(angle))
        dot_y = y0 + 18 + 5 * math.sin(math.radians(angle))
        canvas.create_oval(dot_x-1, dot_y-1, dot_x+1, dot_y+1, fill="#FFFFFF", outline="")

    x0, y0, x1, y1 = cell_bounds(2, 0)
    def draw_baguette(cx, cy, w=78, h=28, rotation=0):
        import math
        # 法棍主体
        if rotation == 0:
            canvas.create_oval(cx-w/2, cy-h/2, cx+w/2, cy+h/2, fill="#D2B48C", outline="#2F2A26")
        else:
            # 创建稍微倾斜的法棍
            angle = math.radians(rotation)
            points = []
            for i in range(0, 360, 10):
                a = math.radians(i)
                x = cx + (w/2 * math.cos(a) * math.cos(angle) - h/2 * math.sin(a) * math.sin(angle))
                y = cy + (w/2 * math.cos(a) * math.sin(angle) + h/2 * math.sin(a) * math.cos(angle))
                points.extend([x, y])
            canvas.create_polygon(points, fill="#D2B48C", outline="#2F2A26")

        for off in (-25, -15, -5, 5, 15, 25):
            canvas.create_arc(cx-14+off, cy-10, cx+14+off, cy+10, start=200, extent=140, style=tk.ARC,
                              outline="#8B6A3E", width=1.5)

        for spot in [(-20, -6), (-5, 7), (15, -5)]:
            canvas.create_oval(cx+spot[0]-2, cy+spot[1]-2, cx+spot[0]+2, cy+spot[1]+2,
                             fill="#8B6A3E", outline="", stipple="gray25")
    
    # 第一根法棍（稍微倾斜）
    draw_baguette((x0+x1)/2+10, (y0+y1)/2-8, rotation=-5)
    # 第二根法棍（相反方向倾斜）
    draw_baguette((x0+x1)/2-8, (y0+y1)/2+10, rotation=10)

    # 中行 1) 羊角面包（更真实的层叠效果）
    x0, y0, x1, y1 = cell_bounds(0, 1)
    cx, cy = (x0+x1)/2, (y0+y1)/2+6
    
    # 底层（稍大）
    canvas.create_arc(cx-54, cy-19, cx+54, cy+19, start=15, extent=150, style=tk.CHORD,
                      fill="#DAB585", outline="#2F2A26")
    # 中间层
    canvas.create_arc(cx-52, cy-18, cx+52, cy+18, start=18, extent=144, style=tk.CHORD,
                      fill="#EBCFA6", outline="#2F2A26")
    # 顶层
    canvas.create_arc(cx-48, cy-16, cx+48, cy+16, start=20, extent=140, style=tk.CHORD,
                      fill="#F5E4C3", outline="#2F2A26")
    
    # 添加层次纹理
    for i, xoff in enumerate([-28, -14, 0, 14, 28]):
        # 不同位置不同长度的线条
        length = 14 - i % 2 * 3
        canvas.create_line(cx+xoff, cy-length/2, cx+xoff, cy+length/2, 
                          fill="#8B6A3E", width=1.5)
    
    # 添加烤焦的边缘效果
    canvas.create_arc(cx-50, cy-17, cx+50, cy+17, start=20, extent=40, style=tk.ARC,
                      outline="#8B6A3E", width=2)
    canvas.create_arc(cx-30, cy-10, cx+10, cy+10, start=110, extent=50, style=tk.ARC,
                      outline="#8B6A3E", width=1.5)

    # 中行 2) 巧克力圆蛋糕（分层设计和更多装饰）
    x0, y0, x1, y1 = cell_bounds(1, 1)
    cx, cy = (x0+x1)/2, (y0+y1)/2+4
    
    # 蛋糕底部阴影
    canvas.create_oval(cx-32, cy-16, cx+32, cy+18, fill="#2A221C", outline="")
    
    # 巧克力蛋糕主体
    canvas.create_oval(cx-30, cy-16, cx+30, cy+16, fill="#3B2E2A", outline="#2F2A26")
    
    # 巧克力纹理
    for i in range(6):
        angle = i * 60
        import math
        start_x = cx + 15 * math.cos(math.radians(angle))
        start_y = cy + 15 * math.sin(math.radians(angle))
        end_x = cx + 28 * math.cos(math.radians(angle))
        end_y = cy + 28 * math.sin(math.radians(angle))
        canvas.create_line(start_x, start_y, end_x, end_y, 
                          fill="#4A3830", width=1)
    
    # 巧克力糖霜顶部
    canvas.create_oval(cx-28, cy-20, cx+28, cy+4, fill="#4A3830", outline="#2F2A26")
    
    # 糖霜装饰（更多更均匀的糖粒）
    colors = ["#E6D9FF", "#FFFFFF", "#F5E6FF", "#E0CCFF"]
    for i in range(12):
        angle = i * 30
        import math
        radius = 18 + (i % 3) * 5
        dot_x = cx + radius * math.cos(math.radians(angle))
        dot_y = cy - 12 + 3 * math.sin(math.radians(angle*2))
        color = colors[i % len(colors)]
        canvas.create_oval(dot_x-1.5, dot_y-1.5, dot_x+1.5, dot_y+1.5, 
                          fill=color, outline="")
    
    # 添加巧克力碎片
    for i, pos in enumerate([(-15, -5), (5, -8), (12, -2)]):
        points = [
            cx+pos[0], cy+pos[1],
            cx+pos[0]+4, cy+pos[1]-2,
            cx+pos[0]+6, cy+pos[1]+3,
            cx+pos[0]+2, cy+pos[1]+5,
        ]
        canvas.create_polygon(points, fill="#5A463C", outline="")

    # 中行 3) 白吐司 + 全麦面包（更真实的质感）
    x0, y0, x1, y1 = cell_bounds(2, 1)
    cx, cy = (x0+x1)/2, (y0+y1)/2
    
    # 白吐司
    # 底部阴影
    canvas.create_rectangle(cx-34, cy-20, cx-6, cy+24, fill="#E8E8E8", outline="")
    # 吐司主体
    canvas.create_rectangle(cx-34, cy-22, cx-6, cy+22, fill="#FFFFFF", outline="#2F2A26")
    # 顶部
    canvas.create_oval(cx-34, cy-28, cx-10, cy-6, fill="#F5E8D6", outline="#2F2A26")
    # 顶部烤痕
    for i in range(4):
        canvas.create_arc(cx-32+i*8, cy-26, cx-28+i*8, cy-22, start=0, extent=180,
                          fill="#D4A76A", outline="")
    
    # 全麦面包
    # 底部阴影
    canvas.create_rectangle(cx, cy-12, x1-6, cy+24, fill="#8A6A4A", outline="")
    # 面包主体
    canvas.create_rectangle(cx, cy-14, x1-6, cy+22, fill="#B48A6A", outline="#2F2A26")
    # 顶部
    canvas.create_oval(cx, cy-22, x1-10, cy, fill="#C49A7A", outline="#2F2A26")
    # 全麦纹理（添加谷物效果）
    for i in range(8):
        grain_x = cx + 4 + (i % 4) * 6
        grain_y = cy - 8 + (i // 4) * 20
        grain_size = 1 + (i % 3) * 0.5
        canvas.create_oval(grain_x-grain_size, grain_y-grain_size, 
                          grain_x+grain_size, grain_y+grain_size, 
                          fill="#8A6A4A", outline="")
    # 面包切口线
    canvas.create_line(cx+6, cy-18, cx+6, cy+20, fill="#8A6A4A", width=1, dash=(2,1))

    # 底行 1) 纸杯蛋糕（精美纸杯和樱桃装饰）
    x0, y0, x1, y1 = cell_bounds(0, 2)
    cx, cy = (x0+x1)/2, (y0+y1)/2+6
    
    # 纸杯（添加条纹和质感）
    cup_points = [cx-34, cy+18, cx+34, cy+18, cx+26, cy-2, cx-26, cy-2]
    canvas.create_polygon(cup_points, fill="#5A463C", outline="#2F2A26")
    # 纸杯条纹
    for i in range(6):
        offset = -30 + i * 10
        canvas.create_line(cx+offset, cy-2, cx+offset*0.7, cy+18, 
                          fill="#6A564C", width=1, dash=(1,2))
    
    # 蛋糕主体
    canvas.create_oval(cx-36, cy-12, cx+36, cy+4, fill="#F5E0B8", outline="#2F2A26")
    # 蛋糕纹理
    for i in range(4):
        canvas.create_arc(cx-34, cy-10+i*3, cx+34, cy+2+i*3, start=180, extent=180,
                          outline="#E5D0A8", width=1)
    
    # 奶油顶部
    canvas.create_oval(cx-30, cy-20, cx+30, cy-4, fill="#FFFFFF", outline="#2F2A26")
    # 奶油漩涡
    for r in range(5, 25, 4):
        canvas.create_arc(cx-r, cy-r-10, cx+r, cy+r-10, start=120, extent=280,
                          outline="#F0F0F0", width=1.5)
    
    # 樱桃
    canvas.create_oval(cx-6, cy-26, cx+6, cy-14, fill="#E45454", outline="#2F2A26")
    # 樱桃高光
    canvas.create_oval(cx-2, cy-24, cx+1, cy-21, fill="#FFFFFF", outline="")
    # 樱桃茎
    canvas.create_line(cx, cy-26, cx-3, cy-32, fill="#8B4513", width=2)
    # 糖粒装饰
    for i in range(10):
        angle = i * 36
        import math
        dot_x = cx + 22 * math.cos(math.radians(angle))
        dot_y = cy - 18 + 4 * math.sin(math.radians(angle*2))
        color = "#FFFFFF" if i % 2 == 0 else "#FFD700"
        canvas.create_oval(dot_x-1, dot_y-1, dot_x+1, dot_y+1, fill=color, outline="")

    # 底行 2) 菠萝包（更精致的网格和质感）
    x0, y0, x1, y1 = cell_bounds(1, 2)
    cx, cy = (x0+x1)/2, (y0+y1)/2+4
    
    # 面包底部阴影
    canvas.create_oval(cx-42, cy-16, cx+42, cy+20, fill="#E5CA60", outline="")
    
    # 面包主体
    canvas.create_oval(cx-40, cy-18, cx+40, cy+18, fill="#F5DC70", outline="#2F2A26")
    
    # 精美的菠萝网格
    # 横向网格线
    for i in range(4):
        y = cy - 14 + i * 8
        # 弯曲的网格线
        canvas.create_arc(cx-50, y-5, cx+50, y+5, start=180, extent=180,
                          outline="#C49B3A", width=3)
    # 纵向网格线
    for i in range(5):
        angle = -60 + i * 30
        import math
        length = 60
        start_x = cx + (length/2) * math.cos(math.radians(angle+180))
        start_y = cy + (length/2) * math.sin(math.radians(angle+180))
        end_x = cx + (length/2) * math.cos(math.radians(angle))
        end_y = cy + (length/2) * math.sin(math.radians(angle))
        canvas.create_line(start_x, start_y, end_x, end_y, fill="#C49B3A", width=3)
    
    # 焦糖斑点
    for i in range(8):
        angle = i * 45
        import math
        radius = 25 + (i % 3) * 5
        spot_x = cx + radius * math.cos(math.radians(angle))
        spot_y = cy + radius * math.sin(math.radians(angle))
        canvas.create_oval(spot_x-3, spot_y-3, spot_x+3, spot_y+3,
                          fill="#A47B2A", outline="", stipple="gray50")
    

    for i in range(3):
        canvas.create_arc(cx-38, cy+12+i*2, cx+38, cy+16+i*2, start=180, extent=180,
                          outline="#E5CA60", width=1)


    x0, y0, x1, y1 = cell_bounds(2, 2)
    cx, cy = (x0+x1)/2, (y0+y1)/2+2

    canvas.create_oval(cx-34, cy-18, cx+34, cy+18, fill="#F5DC70", outline="#2F2A26")

    canvas.create_arc(cx-34, cy-18, cx+34, cy+18, start=340, extent=200, style=tk.CHORD,
                      fill="#4B3A36", outline="#2F2A26")

    for i in range(5):
        angle = 340 + i * 40
        import math
        start_x = cx + 20 * math.cos(math.radians(angle))
        start_y = cy + 20 * math.sin(math.radians(angle))
        end_x = cx + 32 * math.cos(math.radians(angle))
        end_y = cy + 32 * math.sin(math.radians(angle))
        canvas.create_line(start_x, start_y, end_x, end_y, 
                          fill="#5B4A46", width=1)

    canvas.create_arc(cx-34, cy-18, cx+34, cy+18, start=160, extent=180, style=tk.CHORD,
                      fill="#F2A6B8", outline="#2F2A26")

    for i in range(3):
        radius = 34 - i*3
        canvas.create_arc(cx-radius, cy-radius, cx+radius, cy+radius, start=160, extent=180, style=tk.CHORD,
                          fill="", outline="#F5B6C8", width=1)
    

    canvas.create_oval(cx-12, cy-6, cx+12, cy+6, fill="#F5DC70", outline="#2F2A26")
    canvas.create_oval(cx-10, cy-4, cx+10, cy+4, fill="#EAEAEA", outline="")

    colors = ["#FFFFFF", "#FFD700", "#FF6B6B", "#4ECDC4"]
    for i in range(12):
        angle = 160 + i * 15
        import math
        length = 8 + (i % 3) * 2
        radius = 25

        x = cx + radius * math.cos(math.radians(angle))
        y = cy + radius * math.sin(math.radians(angle))

        end_x = x + length * math.cos(math.radians(angle))
        end_y = y + length * math.sin(math.radians(angle))
        color = colors[i % len(colors)]
        canvas.create_line(x, y, end_x, end_y, fill=color, width=2)

        canvas.create_oval(end_x-1.5, end_y-1.5, end_x+1.5, end_y+1.5, fill=color, outline="")
    
    for i in range(8):
        angle = 160 + i * 22
        import math
        radius = 30 + (i % 3) * 2
        x = cx + radius * math.cos(math.radians(angle))
        y = cy + radius * math.sin(math.radians(angle))
        canvas.create_oval(x-1, y-1, x+1, y+1, fill="#FFFFFF", outline="")

    return None
